package business

import enums.Direction
import ext.checkCollision
import model.IView

/**
 * 运动的物体
 * */
interface Moveable : IView {
    /** 当前方向 */
    val currentDirection: Direction

    /** 移动速度 */
    val speed:Int

    /** 会发生碰撞的方向*/
    var badDirection:Direction?

    /** 移动 */
    fun move(direction: Direction)

    /** 判断是否将要碰撞*/
    fun willCollision(block: Blockable):Direction? {
        /** 判断将要发生碰撞*/
        var x=this.x
        var y=this.y
        when(currentDirection){
            Direction.UP->y-=speed
            Direction.DOWN->y+=speed
            Direction.LEFT->x-=speed
            Direction.RIGHT->x+=speed
        }

        /** 越界判断,防止坦克在碰到边界时停止 */
        if(x<0) return currentDirection
        if(GameConfig.WINDOW_WIDTH-this.width<x) return currentDirection
        if(y<0) return currentDirection
        if(GameConfig.WINDOW_HEIGHT-this.height<y) return currentDirection

        var  isCollision=checkCollision(block)

        return if(isCollision) currentDirection else null
    }

    /** 通知将要碰撞的物体和碰撞的方向 */
    fun notifyCollision(block:Blockable?,direction: Direction?){
        badDirection=direction;
    }
}